<template>
	<view class="settings-page">
		<view class="wrapper">
			<view class="set_nav">
				<!-- 个人信息 -->
				<view class="settings-item" @click="goToProfile" >
					<text class="item-text">个人信息</text>
					<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
				</view>
				
				<!-- 登录设置 -->
				<view class="settings-item" @click="goToLoginSettings">
					<text class="item-text">登录设置</text>
					<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
				</view>
				
				<!-- 支付设置 -->
				<view class="settings-item" @click="setPayPassword">
					<text class="item-text">支付设置</text>
					<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
				</view>
				
				<!-- 地址管理 -->
				<view class="settings-item" @click="goToAddressManagement">
					<text class="item-text">地址管理</text>
					<view class="item-desc">
						<text class="desc-text">添加收货地址</text>
						<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
					</view>
				</view>
				
				<!-- 邀请人 -->
				<view class="settings-item">
					<text class="item-text">邀请人</text>
					<text class="invite-text">{{ inviterInfo }}</text>
				</view>
				
				<!-- 实名认证 -->
				<view class="settings-item" @click="goToIdentityAuth">
					<text class="item-text">实名认证</text>
					<view class="item-desc">
						<text class="identity-tip" :style="{color: identityTipColor}">{{ identityTipText }}</text>
						<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
					</view>
				</view>
				
				<!-- 意见反馈 -->
				<view class="settings-item" @click="goToFeedback">
					<text class="item-text">意见反馈</text>
					<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
				</view>
				
				<!-- 清除本地缓存 -->
				<view class="settings-item" @click="clearCache">
					<text class="item-text">清除本地缓存</text>
					<text class="cache-size">{{ cacheSize }}</text>
				</view>
				
				<!-- 关于我们 -->
				<view class="settings-item" @click="goToAbout">
					<text class="item-text">关于我们</text>
					<image :src="serverAddress+'img/icon-back1.png'" class="arrow-icon" alt="更多" />
				</view>
			</view>
			
			<!-- 退出当前账号 -->
			<view class="sign_out" @click="logout">
				<text>退出当前账号</text>
			</view>
		</view>
	</view>
</template>

<script>
import api from '@/api/index.js'
import { getToken, getCurrentUser, clearLoginInfo } from '@/api/auth.js'
import {
		inject
	} from 'vue'
export default {
	data() {
		return {
			serverAddress: inject('$serverAddress'),
			cacheSize: '8.98M',
			inviterInfo: '', // 邀请人信息
			identityTipText: '', // 实名认证提示
			identityTipColor: '#FF5033' // 实名认证提示颜色
		}
	},
	onLoad() {
		this.loadUserSettings()
		this.checkIdentityStatus()
	},
	methods: {
		// 加载用户设置信息
		async loadUserSettings() {
			try {
				const result = await api.settingsApi.getUserSettings()
				if (result && !result.r) {
					const data = result.data
					// 处理手机号隐藏显示
					const formatPhoneNum = (pnum) => {
						return pnum ? pnum.slice(0, 3) + "****" + pnum.slice(7) : ''
					}
					this.inviterInfo = data.recommend ? formatPhoneNum(data.recommend) : (data.nickname || '')
				}
			} catch (error) {
				console.error('加载用户设置失败:', error)
			}
		},
		
		// 检查实名认证状态
		async checkIdentityStatus() {
			try {
				const result = await api.commonApi.getIdentityInfo()
				if (result && result.data) {
					if (result.data.is_checked == 1) {
						this.identityTipText = '已实名认证'
						this.identityTipColor = '#27B387'
					} else {
						this.identityTipText = '未实名认证'
						this.identityTipColor = '#FF5033'
					}
				}
			} catch (error) {
				console.error('检查实名认证状态失败:', error)
				this.identityTipText = '未实名认证'
			}
		},
		
		// 个人信息
		goToProfile() {
			uni.navigateTo({
				url: '/pages/my/profile'
			})
		},
		
		// 登录设置
		goToLoginSettings() {
			uni.navigateTo({
				url: '/pages/my/settings-login'
			})
		},
		
		// 支付设置
		async setPayPassword() {
			try {
				const result = await api.commonApi.checkPayPassword()
				if (result && result.data) {
					if (result.data.is_set == 1) {
						// 已设置支付密码，跳转到支付设置页面
						uni.navigateTo({
							url: '/pages/my/set-zhifu'
						})
					} else {
						// 未设置支付密码，提示用户先设置
						uni.showModal({
							title: '提示',
							content: '您还未设置支付密码，是否现在设置？',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/payment/set-password?setpwd=yes'
									})
								}
							}
						})
					}
				}
			} catch (error) {
				console.error('检查支付密码失败:', error)
				uni.showToast({
					title: '网络错误，请稍后重试',
					icon: 'none'
				})
			}
		},
		
		// 地址管理
		goToAddressManagement() {
			uni.navigateTo({
				url: '/pages/address/index'
			})
		},
		
		// 实名认证
		async goToIdentityAuth() {
			try {
				const result = await api.commonApi.getIdentityInfo()
				if (result && result.data && result.data.is_checked == 1) {
					uni.showToast({
						title: '您已通过实名认证',
						icon: 'success'
					})
					return
				}
				
				// 跳转到实名认证页面
				uni.navigateTo({
					url: '/pages/identity/realNameAuth'
				})
			} catch (error) {
				console.error('实名认证检查失败:', error)
				uni.navigateTo({
					url: '/pages/identity/realNameAuth'
				})
			}
		},
		
		// 意见反馈
		goToFeedback() {
			uni.navigateTo({
				url: '/pages/feedback/index'
			})
		},
		
		// 清除本地缓存
		clearCache() {
			uni.showModal({
				title: '清除缓存',
				content: '确定要清除所有缓存数据吗？',
				success: (res) => {
					if (res.confirm) {
						uni.showLoading({
							title: '清除中...'
						})
						
						// 清除缓存逻辑
						setTimeout(() => {
							uni.hideLoading()
							this.cacheSize = '0MB'
							uni.showToast({
								title: '清除成功',
								icon: 'success'
							})
						}, 1000)
					}
				}
			})
		},
		
		// 关于我们
		goToAbout() {
			uni.navigateTo({
				url: '/pages/my/set-guanyu'
			})
		},
		
		// 退出登录
		logout() {
			uni.showModal({
				title: '退出登录',
				content: '确定要退出当前账号吗？',
				confirmColor: '#ff4757',
				success: (res) => {
					if (res.confirm) {
						// 清除用户信息和token
						clearLoginInfo()
						
						// 跳转到个人中心页面
						uni.reLaunch({
							url: '/pages/my/index'
						})
					}
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.settings-page {
	background-color: #f7f7f7;
	height: 100vh;
}

.wrapper {
	background-color: white;
	font-size: 28rpx;
	padding-top: 20rpx;
}

.set_nav {
	padding: 0 30rpx;
	background-color: white;
}

.settings-item {
	padding: 30rpx 0;
	border-bottom: 1px solid #E8E8E8;
	display: flex;
	align-items: center;
	justify-content: space-between;
	
	&:last-child {
		border-bottom: 0px;
	}
}

.item-text {
	color: #333333;
	font-size: 28rpx;
}

.item-desc {
	display: flex;
	align-items: center;
	gap: 16rpx;
}

.desc-text {
	color: #666666;
	font-size: 24rpx;
}

.arrow-icon {
	width: 14rpx;
	height: 25rpx;
}

.invite-text {
	margin-right: 40rpx;
	font-size: 25rpx;
	color: #666666;
}

.identity-tip {
	font-size: 24rpx;
}

.cache-size {
	color: #666666;
	font-size: 24rpx;
}

/* 退出当前登录按钮 */
.sign_out {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 526rpx;
	height: 80rpx;
	border-radius: 10rpx;
	margin: 94rpx auto 0;
	color: white;
	background-color: #FF5033;
	font-size: 25rpx;
	font-weight: bold;
}
</style>